package com.rimi.service.impl;

import com.rimi.bean.Video;
import com.rimi.bean.Watch;
import com.rimi.cache.AdminCache;
import com.rimi.cache.VideoCache;
import com.rimi.dao.VideoDao;
import com.rimi.dao.WatchDao;
import com.rimi.service.WatchService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * InnoDB free: 11264 kB; (`user_id`) REFER `owl_video/user`(`user_id`); (`video_id 服务实现类
 * </p>
 *
 * @author sky
 * @since 2018-06-06
 */
@Service
public class WatchServiceImpl extends ServiceImpl<WatchDao, Watch> implements WatchService {
    @Autowired
    private WatchDao watchDao;
    @Autowired
    private VideoDao videoDao;
    @Autowired
    private VideoCache videoCache;


    @Override
    public List<Video> getCollectionByUserId(Integer userId) {
        List<Video> collection = videoCache.getCollectionByUserId(userId);
        if (collection != null){
            return collection;
        }else {
            List<String> collect = watchDao.getVideoCollectArrayByUserId(userId);
            String ss = "";
            for (int i = 0; i < collect.size(); i++) {
                ss += collect.get(i)+",";
            }
            ss=ss.substring(0,ss.length()-1);
            collection = videoDao.getVideoCollectionListByIds(ss);
            videoCache.addCollectionByUserId(userId,collection);
            return collection;
        }
    }

    @Override
    public List<Video> getWatchHistory(Integer userId) {
        List<Video> history = videoCache.getHistoryByUserId(userId);
        if(history != null){
            return history;
        }else {
            List<Watch> watchHistory = watchDao.getHistoryByUserId(userId);
            String ss = "";
            for(int i = 0; i < watchHistory.size(); i++){
                ss += watchHistory.get(i).getVideoId()+",";
            }
            ss=ss.substring(0,ss.length()-1);
            history = videoDao.getVideoHistoryByVideoId(ss);
            videoCache.addHistory(userId,history);
            return history;
        }
    }
}
